#include "DGL3Common.glsl"
#include "DGL3ProgramEnums.inc"

//-----------------------------------------

struct VisibleInfo 
{
  int vertexOut;
  int fragmentIn;
};

uniform int uIndex;
 
#ifdef VERTEX_SHADER

	layout(location = GE_VA_Position) in vec3 aPosition;
	layout(location = GE_VA_Transform) in ivec2 aAssigns;

	layout(std430, binding = GE_VA_Texture) writeonly buffer visibleBuffer
	{
		VisibleInfo uVisible[];
	};
	
	bool IsInisde(float x, float y, float l, float r, float b, float t)
	{
		return x > l && x < r && y > b && y < t;
	}
	
	void main(void)
	{
		gl_Position = uProjectionMatrix * uViewMatrix * GetModelMatrix(aAssigns.x) * vec4(aPosition, 1.0);
		
		vec4 ndcPosition =  gl_Position;
		ndcPosition = ndcPosition / ndcPosition.w;
		
		 if (!IsInisde(ndcPosition.x, ndcPosition.y, -1.0, 1.0, -1.0, 1.0))
		 {
			 uVisible[uIndex].vertexOut = 1;
		 }
	}

#endif

#ifdef FRAGMENT_SHADER

	layout(early_fragment_tests) in;

	layout(std430, binding = GE_VA_Texture) writeonly buffer visibleBuffer
	{
		VisibleInfo uVisible[];
	};

	layout(location = 0) out vec4 fragColor;

	void main()
	{
		uVisible[uIndex].fragmentIn = 1;
		fragColor = vec4(1.0);
	}

#endif
